package com.xiaoyu.izf.controller;

import com.xiaoyu.izf.aspect.RequestAuth;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.YqfStoreStatsDay;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.repository.YqfStoreSatsDayDao;
import com.xiaoyu.izf.service.YqfCommissionSerialLogService;
import com.xiaoyu.izf.service.YqfDataGetService;
import com.xiaoyu.izf.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/yqf_commission_serial")
public class YqfCommissionSerialLogController extends BaseController {

    @Autowired
    private YqfDataGetService yqfDataGetService;
    @Autowired
    private YqfStoreSatsDayDao yqfStoreSatsDayDao;
    @Autowired
    private YqfCommissionSerialLogService yqfCommissionSerialLogService;

    @RequestMapping("/test")
    @ResponseBody
    public Object test() {
        YqfStoreStatsDay day = yqfStoreSatsDayDao.findOneById(1);
        yqfDataGetService.computeSerialCommission(day);
        return "OK";
    }

    @RequestMapping("/par_list_page")
    @RequestAuth
    public String parListPage(Model model) {
        String today = DateTimeUtil.getDate2Str();
        String timeFrame = today.substring(0, 8) + "01至" + today;
        model.addAttribute("timeFrame", timeFrame);
        return "mgr/bmc_stats/par_list";
    }

    @RequestMapping("/par_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean parList(HttpServletRequest request, @RequestBody String body) {
        UserInfo user = user(request);
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = yqfCommissionSerialLogService.parList(paramListBean, user);
        return dataTableBean;
    }

    @RequestMapping("/list_page")
    @RequestAuth
    public String listPage(Model model) {
        String today = DateTimeUtil.getDate2Str();
        String timeFrame = today.substring(0, 8) + "01至" + today;
        model.addAttribute("timeFrame", timeFrame);
        return "mgr/bmc_stats/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    @RequestAuth
    public DataTableBean list(HttpServletRequest request, @RequestBody String body) {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = yqfCommissionSerialLogService.list(paramListBean);
        return dataTableBean;
    }
}
